<?php

namespace App\Http\Model;

use App\Http\Services\Message\MessageServices;

/**
 * 系统配置
 *
 * Created on 2021/10/26 12:08
 * @Author <yuxiang.zhu@asiainnovations.net>
 */
class SystemConfig extends Base
{
    /**
     * 表名字
     *
     * @var string
     */
    public $table = 'system_config';

    /**
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 获取系统默认配置
     *
     * Created at 2021/10/26 12:17
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return array
     */
    public function getDefaultSystemConfig(): array
    {
        $configs = (new System())->get_config();
        return $configs ?: [];
    }

    /**
     * 用户等级提升是否展示给房间全部用户
     * 用户财富值至少需要达到10万(默认)
     *
     * Created at 2021/11/11 17:14
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return int
     */
    public function getLevelUpMsgShowToAllMinWealthBalance(): int
    {
        $key = 'level_up_msg_show_to_all_min_wealth_balance';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? intval($configs[$key]['value']) : 100000;
    }

    /**
     * 用户等级提升是否展示给房间全部用户
     * 用户魅力值至少需要达到10万(默认)
     *
     * Created at 2021/11/11 17:14
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return int
     */
    public function getLevelUpMsgShowToAllMinCharm(): int
    {
        $key = 'level_up_msg_show_to_all_min_charm';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? intval($configs[$key]['value']) : 100000;
    }

    /**
     * 靓号商城是否开启
     *
     * Created at 2021/12/7 17:09
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return int
     */
    public function getCoolNumberShoppingMallOpenStatus(): int
    {
        $key = 'cool_number_shopping_mall_open_status';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? intval($configs[$key]['value']) : 0;
    }

    /*
     * 过滤-白名单, 返回的是用户uuid
     *
     *
     * Created at 2021/12/23 15:25
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return array|int[]
     */
    public function getUserFilterWhiteList(): array
    {
        $key = 'user_filter_uuid_white_list';
        $configs = $this->getDefaultSystemConfig();
        $list = isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
        $userUuids = [];
        foreach ($list as $item) {
            $userUuids[] = trim($item);
        }

        return $userUuids ?: [];
    }

    /**
     * 过滤-黑名单, 返回的是用户uuid
     *
     *
     * Created at 2021/12/23 15:25
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return array|int[]
     */
    public function getUserFilterBlackList(): array
    {
        $key = 'user_filter_uuid_black_list';
        $configs = $this->getDefaultSystemConfig();
        $list = isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
        $userUuids = [];
        foreach ($list as $item) {
            $userUuids[] = trim($item);
        }

        return $userUuids ?: [];
    }

    /**
     * 过滤-黑名单, 返回的是房间id
     *
     *
     * Created at 2021/12/23 15:25
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return array|int[]
     */
    public function getRoomFilterBlackList(): array
    {
        $key = 'room_filter_id_black_list';
        $configs = $this->getDefaultSystemConfig();
        $list = isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
        $roomUuids = [];
        foreach ($list as $item) {
            $roomUuids[] = trim($item);
        }

        return $roomUuids ?: [];
    }

    /**
     * 过滤-黑名单, 返回的是城市列表
     *
     *
     * Created at 2021/12/23 15:25
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return array|int[]
     */
    public function getAddressFilterBlackList(): array
    {
        $key = 'address_filter_black_list';
        $configs = $this->getDefaultSystemConfig();
        $list = isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
        $listAddress = [];
        foreach ($list as $item) {
            $listAddress[] = trim($item);
        }

        return $listAddress ?: [];
    }

    /**
     * 兑换来来币和提现黑名单
     * Created at 2022/01/25
     * @Author zxiaofee
     * @return array
     */
    public function getExchangeAndWithdrawUserBlacklist(): array
    {
        $key = 'finance_exchange_and_withdraw_blacklist';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
    }

    /**
     * 获取非法用户名单
     * Created at 2022/02/24
     * @Author zxiaofee
     * @return array
     */
    public function getConsumeUserBlacklist(): array
    {
        $key = 'finance_consume_user_blacklist';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
    }

    /**
     * 获取非法用户名单--白名单
     * Created at 2022/03/02
     * @Author wj
     * @return array
     */
    public function getConsumeUserWhitelist(): array
    {
        $key = 'finance_consume_user_whitelist';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
    }

    /**
     * 获取用户充值限制
     * Created at 2022/04/01
     * @Author zxiaofee
     * @return ?int
     */
    public function getRechargeTopLimit(): ?int {
        $key = 'finance_recharge_consume_toplimit';
        $configs = $this->getDefaultSystemConfig();
        $arr = isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
        if (empty($arr)) {
            return null;
        }
        return $arr[0] ?? null;
    }
    /**
     * 获取用户消费限制
     * Created at 2022/04/01
     * @Author zxiaofee
     * @return ?int
     */
    public function getConsumeTopLimit(): ?int {
        $key = 'finance_recharge_consume_toplimit';
        $configs = $this->getDefaultSystemConfig();
        $arr = isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
        if (empty($arr)) {
            return null;
        }
        return $arr[1] ?? null;
    }

    public function getForbiddenTimeForUpdate(){
        $key = 'forbidden_time_for_update';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [0,0];
    }

    public function getUnionConsumerLogShowConfig(){
        $key = 'union_consumer_log_show_config';
        $configs = $this->getDefaultSystemConfig();
        $configs = isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [0,0];
        $ret = [0, 0];
        $ret[0] = $configs[0] ?? 0;
        $ret[1] = $configs[1] ?? 0;
        return $ret;
    }

    public function getForbidRegisterAndVerifyList(){
        $key = 'forbid_register_verify_list';
        $configs = $this->getDefaultSystemConfig();
        $configs = isset($configs[$key]) ? explode('|', $configs[$key]['value']) : [0,0];
        $ret = [0, 0];
        $ret[0] = $configs[0] ?? 0;
        $ret[1] = $configs[1] ?? 0;
        return $ret;
    }

    /**
     * 私聊开关，如果未实名且财富值小于500时，不允许发私信
     * @return int
     */

    public function getPrivateCertifyWealthBalanceSwitch(): int
    {
        $key = 'private_certify_wealthBalance_switch';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? intval($configs[$key]['value']) : 0;
    }

    //获取全部房间关闭和全员闭麦配置
    public function getRoomAndVolumeCloseConfig(){
        $key = 'room_and_volume_close_config';
        $configs = $this->getDefaultSystemConfig();
        $configs = isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [0,0,0,0];
        $ret = [0, 0, 0, 0];
        $ret[0] = $configs[0] ?? 0;
        $ret[1] = $configs[1] ?? 0;
        $ret[2] = $configs[2] ?? 0;
        $ret[3] = $configs[3] ?? 0;
        return $ret;
    }

    public function getHotRoomListShowConfig(){
        $key = 'hot_roomList_show_config';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? intval($configs[$key]['value']) : 0;
    }

    /**
     * 私聊白名单配置
     *
     */
    public function getPrivateMsgWhiteConfig()
    {
        $key = 'private_msg_white_config';
        $configs = $this->getDefaultSystemConfig();
        return empty($configs[$key]) ? [] : json_decode($configs[$key]['value'], true);
    }

    /**
     * 私聊及搜索限制配置
     *
     */
    public function getPrivateMsgBlackConfig()
    {
        $key = 'private_msg_block_config';
        $configs = $this->getDefaultSystemConfig();
        return empty($configs[$key]) ? [] : json_decode($configs[$key]['value'], true);
    }

    /**
     * 黑名单语言配置
     *
     */
    public function getBlackWordConfig()
    {
        $key = 'black_word_config';
        $configs = $this->getDefaultSystemConfig();
        return empty($configs[$key]) ? [] : json_decode($configs[$key]['value'], true);
    }

    /**
     * 过滤-黑名单, 进入房间限制配置
     * 配置内：
     * maximum_gold_block_user : 不能进入最多金房间的用户uuid
     * maximum_gold_block_city : 不能进入最多金房间的城市
     * @return array
     */
    public function getRoomBlockConfig(): array
    {
        $key = 'room_block_config';
        $configs = $this->getDefaultSystemConfig();
        $data = null;
        if (isset($configs[$key])) {
            $data = json_decode($configs[$key]['value'], 1);
        }

        if ($data === null) {
            MessageServices::sendInfo("room_block_config 房间限制json解析报错，拦截失效", 'warn');
        }
        return $data;
    }

    /**
     * 获取万能登录短信验证码配置
     * @return string
     */
    public function getUniversalCodeConfig()
    {
        $key = 'universal_code';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? $configs[$key]['value'] : '';
    }

    /**
     * 获取cibn支付配置
     * @return array
     */
    public function getPayCibnConfig(): array
    {
        $key = 'pay_cibn';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
    }
    /**
     * 获取盛付通支付配置
     * @return array
     */
    public function getPaySftConfig(): array
    {
        $key = 'pay_sft';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
    }

    /**
     * 获取百富支付配置
     * @return array
     */
    public function getPayWeibaoConfig(): array
    {
        $key = 'pay_weibao';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
    }

    public function getSmsChannel()
    {
        $key = 'sms_channel';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? $configs[$key]['value'] : '';
    }

    /**
     * 获取常富支付配置
     * @return array
     */
    public function getPayZftH5Config(): array
    {
        $key = 'pay_ZftH5';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
    }

    public function getPayZftAppConfig(): array
    {
        $key = 'pay_ZftApp';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
    }

    //TODO :支付宝支付
    public function getAlipayCertPath()
    {
        $key = 'pay_alipay_cfg';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? $configs[$key]['value'] : '';
    }

    public function getPayMinpriceRealnamet()
    {
        $key = 'pay_minprice_realname';
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? $configs[$key]['value'] : '';
    }

    //TODO :微信支付
    public function getPayWxApp1()
    {
        $key = 'pay_wx_app_1';
        $bigkey = 'sys_wechat_pay_mch_ids';
        $configs = $this->getDefaultSystemConfig();
        $bigconfig = isset($configs[$bigkey]) ? $configs[$bigkey]['value'] : '';
        if (empty($bigconfig)) {
            return '';
        }
        $bigconfig = json_decode($bigconfig, true);
        if (!isset($bigconfig['list'])) {
            return '';
        }
        $value = "";
        foreach ($bigconfig['list'] as $item) {
            if ($item['key'] == $key) {
                $value = $item['value'];
            }
        }
        return $value;
    }
    //TODO :微信支付
    public function getPayWxApp2()
    {
        $key = 'pay_wx_app_2';
        $bigkey = 'sys_wechat_pay_mch_ids';
        $configs = $this->getDefaultSystemConfig();
        $bigconfig = isset($configs[$bigkey]) ? $configs[$bigkey]['value'] : '';
        if (empty($bigconfig)) {
            return '';
        }
        $bigconfig = json_decode($bigconfig, true);
        if (!isset($bigconfig['list'])) {
            return '';
        }
        $value = "";
        foreach ($bigconfig['list'] as $item) {
            if ($item['key'] == $key) {
                $value = $item['value'];
            }
        }
        return $value;
    }
    //TODO :微信支付
    public function getPayWxApp3()
    {
        $key = 'pay_wx_app_3';
        $bigkey = 'sys_wechat_pay_mch_ids';
        $configs = $this->getDefaultSystemConfig();
        $bigconfig = isset($configs[$bigkey]) ? $configs[$bigkey]['value'] : '';
        if (empty($bigconfig)) {
            return '';
        }
        $bigconfig = json_decode($bigconfig, true);
        if (!isset($bigconfig['list'])) {
            return '';
        }
        $value = "";
        foreach ($bigconfig['list'] as $item) {
            if ($item['key'] == $key) {
                $value = $item['value'];
            }
        }
        return $value;
    }

    public function getKeyConfig($key)
    {
        $configs = $this->getDefaultSystemConfig();
        return isset($configs[$key]) ? explode(',', $configs[$key]['value']) : [];
    }
}